<template>
  <div class="login-container">
    <!-- 背景装饰 -->
    <div class="background-decoration">
      <div class="floating-shapes">
        <div class="shape shape-1"></div>
        <div class="shape shape-2"></div>
        <div class="shape shape-3"></div>
        <div class="shape shape-4"></div>
        <div class="shape shape-5"></div>
        <div class="shape shape-6"></div>
        <div class="shape shape-7"></div>
      </div>
    </div>

    <div class="login-wrapper">
      <div class="login-card">
        <!-- Logo 区域 -->
        <div class="logo-section">
          <div class="logo">
            <svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
              <path d="M12 2L2 7L12 12L22 7L12 2Z" stroke="currentColor" stroke-width="2" stroke-linejoin="round"/>
              <path d="M2 17L12 22L22 17" stroke="currentColor" stroke-width="2" stroke-linejoin="round"/>
              <path d="M2 12L12 17L22 12" stroke="currentColor" stroke-width="2" stroke-linejoin="round"/>
            </svg>
          </div>
          <h1 class="login-title">欢迎回来</h1>
          <p class="login-subtitle">请登录您的账户</p>
        </div>

        <!-- 登录表单 -->
        <el-form :model="form" class="login-form" @submit.prevent="onSubmit">
          <el-form-item class="form-item username-field">
            <el-input 
              v-model="form.username" 
              placeholder="请输入用户名" 
              clearable
              class="custom-input"
              size="large"
            >
              <template #prefix>
                <svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                  <path d="M20 21V19C20 17.9391 19.5786 16.9217 18.8284 16.1716C18.0783 15.4214 17.0609 15 16 15H8C6.93913 15 5.92172 15.4214 5.17157 16.1716C4.42143 16.9217 4 17.9391 4 19V21" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                  <circle cx="12" cy="7" r="4" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                </svg>
              </template>
            </el-input>
          </el-form-item>


          <!-- 记住我 -->
          <div class="remember-forgot">
            <el-checkbox v-model="rememberMe" class="remember-checkbox">
              记住我
            </el-checkbox>
            <a href="#" class="forgot-link">新用户注册</a>
          </div>

          <el-form-item class="form-item login-button-field">
            <el-button 
              type="primary" 
              @click="onSubmit" 
              :loading="loading"
              class="login-button"
              size="large"
            >
              <span v-if="!loading">登录</span>
              <span v-else>登录中...</span>
            </el-button>
          </el-form-item>

          <div v-if="error" class="error-message">
            <svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
              <circle cx="12" cy="12" r="10" stroke="currentColor" stroke-width="2"/>
              <line x1="15" y1="9" x2="9" y2="15" stroke="currentColor" stroke-width="2"/>
              <line x1="9" y1="9" x2="15" y2="15" stroke="currentColor" stroke-width="2"/>
            </svg>
            {{ error }}
          </div>
        </el-form>

        <!-- 分割线 -->
        <div class="divider">
          <span>或</span>
        </div>

        <!-- 社交登录 -->
        <div class="social-login">
          <el-button class="social-button google-button" size="large">
            <svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
              <path d="M22.56 12.25c0-.78-.07-1.53-.2-2.25H12v4.26h5.92c-.26 1.37-1.04 2.53-2.21 3.31v2.77h3.57c2.08-1.92 3.28-4.74 3.28-8.09z" fill="#4285F4"/>
              <path d="M12 23c2.97 0 5.46-.98 7.28-2.66l-3.57-2.77c-.98.66-2.23 1.06-3.71 1.06-2.86 0-5.29-1.93-6.16-4.53H2.18v2.84C3.99 20.53 7.7 23 12 23z" fill="#34A853"/>
              <path d="M5.84 14.09c-.22-.66-.35-1.36-.35-2.09s.13-1.43.35-2.09V7.07H2.18C1.43 8.55 1 10.22 1 12s.43 3.45 1.18 4.93l2.85-2.22.81-.62z" fill="#FBBC05"/>
              <path d="M12 5.38c1.62 0 3.06.56 4.21 1.64l3.15-3.15C17.45 2.09 14.97 1 12 1 7.7 1 3.99 3.47 2.18 7.07l3.66 2.84c.87-2.6 3.3-4.53 6.16-4.53z" fill="#EA4335"/>
            </svg>
            使用 Google 登录
          </el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { login } from '@/api/login'
import { useUserStore } from "@/stores/user"

const emit = defineEmits(['login-success'])
const router = useRouter()
const route = useRoute()
const userStore = useUserStore()
const form = reactive({ username: '' })
const loading = ref(false)
const error = ref('')
const rememberMe = ref(false)

function validate() {
  if (!form.username.trim()) {
    error.value = '请输入用户名'
    return false
  }
  error.value = ''
  return true
}

async function onSubmit() {
  if (!validate()) return
  loading.value = true

  // 使用API登录
  const response = await login(form.username)
  console.log("response",response)
  if(response.code !== 200){
    loading.value = false
    return
  }

  // 处理响应数据
  const userData = response.data || response
  
  // 保存用户信息到localStorage
  const userInfo = {
    userId: userData.id || userData.userId || parseInt(form.username) || 1,
    userName: form.username,
  }

  // 设置用户信息
  userStore.setUserInfo(userInfo)

  emit('login-success', userData)
  
  // 跳转到目标页面或默认页面
  const redirectPath = route.query.redirect as string
  if (redirectPath) {
    router.push(redirectPath)
  } else {
    router.push({ name: 'Posts' })
  }
}
</script>

<style scoped>
/* 全局样式重置，确保全屏显示 */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html, body {
  height: 100%;
  width: 100%;
  overflow: hidden;
}
/* 主容器 */
.login-container {
  min-height: 100vh;
  height: 100vh;
  width: 100vw;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  position: fixed;
  top: 0;
  left: 0;
  overflow: hidden;
  padding: 0;
  margin: 0;
}

/* 背景装饰 */
.background-decoration {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  z-index: 1;
}

.floating-shapes {
  position: absolute;
  width: 100%;
  height: 100%;
}

.shape {
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  animation: float 6s ease-in-out infinite;
}

.shape-1 {
  width: 120px;
  height: 120px;
  top: 10%;
  left: 5%;
  animation-delay: 0s;
}

.shape-2 {
  width: 160px;
  height: 160px;
  top: 50%;
  right: 10%;
  animation-delay: 2s;
}

.shape-3 {
  width: 80px;
  height: 80px;
  bottom: 15%;
  left: 15%;
  animation-delay: 4s;
}

.shape-4 {
  width: 140px;
  height: 140px;
  top: 25%;
  right: 25%;
  animation-delay: 1s;
}

.shape-5 {
  width: 100px;
  height: 100px;
  top: 70%;
  left: 60%;
  animation-delay: 3s;
}

.shape-6 {
  width: 90px;
  height: 90px;
  top: 15%;
  left: 70%;
  animation-delay: 5s;
}

.shape-7 {
  width: 70px;
  height: 70px;
  bottom: 30%;
  right: 40%;
  animation-delay: 1.5s;
}

@keyframes float {
  0%, 100% {
    transform: translateY(0px) rotate(0deg);
  }
  50% {
    transform: translateY(-20px) rotate(180deg);
  }
}

/* 登录包装器 */
.login-wrapper {
  position: relative;
  z-index: 2;
  width: 100%;
  max-width: 500px;
  padding: 20px;
}

/* 登录卡片 */
.login-card {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20px);
  border-radius: 24px;
  padding: 48px;
  box-shadow: 
    0 25px 50px rgba(0, 0, 0, 0.15),
    0 0 0 1px rgba(255, 255, 255, 0.2);
  border: 1px solid rgba(255, 255, 255, 0.3);
  animation: slideUp 0.6s ease-out;
}

@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* Logo 区域 */
.logo-section {
  text-align: center;
  margin-bottom: 40px;
}

.logo {
  width: 80px;
  height: 80px;
  margin: 0 auto 20px;
  background: linear-gradient(135deg, #667eea, #764ba2);
  border-radius: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  animation: pulse 2s infinite;
}

.logo svg {
  width: 40px;
  height: 40px;
}

@keyframes pulse {
  0%, 100% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.05);
  }
}

.login-title {
  font-size: 32px;
  font-weight: 700;
  color: #1a1a1a;
  margin: 0 0 12px 0;
  background: linear-gradient(135deg, #667eea, #764ba2);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.login-subtitle {
  font-size: 18px;
  color: #666;
  margin: 0;
  font-weight: 400;
}

/* 表单样式 */
.login-form {
  margin: 0;
}

.form-item {
  margin-bottom: 24px;
}

.username-field,
.login-button-field {
  margin-bottom: 24px;
}

/* 确保输入框和按钮完美对齐 */
.username-field :deep(.el-form-item__content),
.login-button-field :deep(.el-form-item__content) {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.custom-input {
  width: 100%;
}

.custom-input :deep(.el-input__wrapper) {
  border-radius: 12px;
  border: 2px solid #e5e7eb;
  padding-left: 16px;
  padding-right: 16px;
  height: 60px;
  background: #fafafa;
  transition: all 0.3s ease;
  box-shadow: none;
}

.custom-input :deep(.el-input__prefix) {
  color: #999;
  transition: color 0.3s ease;
}

.custom-input :deep(.el-input__prefix svg) {
  width: 20px;
  height: 20px;
}

.custom-input :deep(.el-input__wrapper:hover) {
  border-color: #667eea;
  background: #fff;
}

.custom-input :deep(.el-input__wrapper.is-focus) {
  border-color: #667eea;
  background: #fff;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

.custom-input :deep(.el-input__wrapper.is-focus .el-input__prefix) {
  color: #667eea;
}

.custom-input :deep(.el-input__inner) {
  font-size: 18px;
  color: #1a1a1a;
}

.custom-input :deep(.el-input__inner::placeholder) {
  color: #999;
}

/* 记住我和注册 */
.remember-forgot {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 32px;
}

.remember-checkbox {
  color: #666;
}

.forgot-link {
  color: #667eea;
  text-decoration: none;
  font-size: 16px;
  font-weight: 500;
  transition: color 0.3s ease;
}

.forgot-link:hover {
  color: #764ba2;
}

/* 登录按钮 */
.login-button {
  width: 100%;
  height: 60px;
  border-radius: 12px;
  background: linear-gradient(135deg, #667eea, #764ba2);
  border: none;
  font-size: 18px;
  font-weight: 600;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(102, 126, 234, 0.3);
}

.login-button:active {
  transform: translateY(0);
}

.login-button :deep(.el-button__loading) {
  color: white;
}

/* 错误信息 */
.error-message {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  color: #ef4444;
  background: #fef2f2;
  border: 1px solid #fecaca;
  border-radius: 8px;
  padding: 12px 16px;
  margin-top: 16px;
  font-size: 14px;
  animation: shake 0.5s ease-in-out;
}

.error-message svg {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-5px); }
  75% { transform: translateX(5px); }
}

/* 分割线 */
.divider {
  position: relative;
  text-align: center;
  margin: 40px 0;
  color: #999;
  font-size: 16px;
}

.divider::before {
  content: '';
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
  height: 1px;
  background: #e5e7eb;
}

.divider span {
  background: rgba(255, 255, 255, 0.95);
  padding: 0 16px;
  position: relative;
  z-index: 1;
}

/* 社交登录 */
.social-login {
  margin-top: 24px;
}

.social-button {
  width: 100%;
  height: 60px;
  border-radius: 12px;
  border: 2px solid #e5e7eb;
  background: white;
  color: #374151;
  font-weight: 500;
  font-size: 16px;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
}

.social-button:hover {
  border-color: #d1d5db;
  background: #f9fafb;
  transform: translateY(-1px);
}

.social-button svg {
  width: 20px;
  height: 20px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .login-container {
    padding: 0;
  }
  
  .login-wrapper {
    max-width: 100%;
    padding: 16px;
  }
  
  .login-card {
    padding: 40px 32px;
    border-radius: 20px;
  }
  
  .login-title {
    font-size: 28px;
  }
  
  .login-subtitle {
    font-size: 16px;
  }
  
  .logo {
    width: 72px;
    height: 72px;
  }
  
  .logo svg {
    width: 36px;
    height: 36px;
  }
  
  .custom-input :deep(.el-input__wrapper) {
    height: 56px;
  }
  
  .custom-input :deep(.el-input__inner) {
    font-size: 16px;
  }
  
  .login-button {
    height: 56px;
    font-size: 16px;
  }
  
  .social-button {
    height: 56px;
    font-size: 15px;
  }
}

@media (max-width: 480px) {
  .login-container {
    padding: 0;
  }
  
  .login-wrapper {
    padding: 12px;
  }
  
  .login-card {
    padding: 32px 24px;
    border-radius: 16px;
  }
  
  .login-title {
    font-size: 24px;
  }
  
  .logo {
    width: 64px;
    height: 64px;
  }
  
  .logo svg {
    width: 32px;
    height: 32px;
  }
}

/* 暗色主题支持 */
@media (prefers-color-scheme: dark) {
  .login-card {
    background: rgba(17, 24, 39, 0.95);
    border: 1px solid rgba(255, 255, 255, 0.1);
  }
  
  .login-title {
    color: #f9fafb;
  }
  
  .login-subtitle {
    color: #9ca3af;
  }
  
  .custom-input :deep(.el-input__wrapper) {
    background: #374151;
    border-color: #4b5563;
  }
  
  .custom-input :deep(.el-input__wrapper:hover),
  .custom-input :deep(.el-input__wrapper.is-focus) {
    background: #4b5563;
  }
  
  .custom-input :deep(.el-input__inner) {
    color: #f9fafb;
  }
  
  .remember-checkbox {
    color: #9ca3af;
  }
  
  .social-button {
    background: #374151;
    border-color: #4b5563;
    color: #f9fafb;
  }
  
  .social-button:hover {
    background: #4b5563;
    border-color: #6b7280;
  }
  
  .divider span {
    background: rgba(17, 24, 39, 0.95);
  }
}
</style>
